﻿using OxyPlot;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automation.BDaq;
using System.Diagnostics;
using Prism.Events;
using DingVision.Infrastructure.Events;

namespace DingVision.ForceTest.ViewModels
{
    public class PressureCurveViewModel : BindableBase
    {
        #region 变量
        private readonly IEventAggregator eventAggregator;
        string deviceDescription = "USB-4704,BID#0";
        //WaveformAiCtrl waveformAiCtrl = new WaveformAiCtrl();

        // Step 1: Create a 'InstantAiCtrl' for Instant AI function.
        InstantAiCtrl instantAIContrl = new InstantAiCtrl();

        ErrorCode errorCode = ErrorCode.Success;
        Stopwatch sw = new Stopwatch();
        double d1 = 0, d2 = 0;
        #endregion
        #region 属性绑定
        private IList<DataPoint> points = new ObservableCollection<DataPoint>();
        public IList<DataPoint> Points
        {
            get { return points; }
            set { SetProperty(ref points, value); }
        }
        private double collectionTimeSpan = 1;
        public double CollectionTimeSpan
        {
            get { return collectionTimeSpan; }
            set { SetProperty(ref collectionTimeSpan, value); }
        }
        private double pressureValue;
        public double PressureValue
        {
            get { return pressureValue; }
            set { SetProperty(ref pressureValue, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand collectionCommand;        
        public DelegateCommand CollectionCommand =>
            collectionCommand ?? (collectionCommand = new DelegateCommand(ExecuteCollectionCommand));

        async void ExecuteCollectionCommand()
        {
            sw.Restart();
            int i = 0;
            Points.Clear();
            while (true)
            {
                Points.Add(new DataPoint(i++, d1 / 16 * 1000));
                await Task.Delay(1);
                if (sw.Elapsed.TotalSeconds > CollectionTimeSpan)
                {
                    break;
                }
            }
           
        }
        #endregion

        public PressureCurveViewModel(IEventAggregator _eventAggregator)
        {
            eventAggregator = _eventAggregator;
            eventAggregator.GetEvent<ShellSendEvent>().Subscribe(ShellSendEventReceived);
        }
        #region 自定义函数
        private void ShellSendEventReceived(string obj)
        {
            switch (obj.ToString())
            {
                case "Loaded":
                    {
                        //waveformAiCtrl.SelectedDevice = new DeviceInformation(deviceDescription);

                        //Conversion conversion = waveformAiCtrl.Conversion;
                        //conversion.ChannelStart = 0;
                        //conversion.ChannelCount = 1;
                        //conversion.ClockRate = 1000;
                        //Record record = waveformAiCtrl.Record;
                        //record.SectionCount = 1;//The 0 means setting 'streaming' mode.
                        //record.SectionLength = 512;
                        //errorCode = waveformAiCtrl.Prepare();

                        instantAIContrl.SelectedDevice = new DeviceInformation(deviceDescription);
                        Task.Run(()=> {
                            while (true)
                            {
                                double[] scaledData = new double[2];
                                instantAIContrl.Read(0, 2, scaledData);
                                d1 = scaledData[0];
                                d2 = scaledData[1];
                            }
                        });
                        Run();
                    }
                    break;
                default:
                    break;
            }
        }
        async void Run()
        {
            while (true)
            {
                PressureValue = d1 / 16 * 1000;
                await Task.Delay(100);
            }
        }
        #endregion
    }
}
